package com.fita.entity.agent;

import com.demo2do.core.format.PersistentFormat;
import com.fasterxml.jackson.annotation.*;
import com.fita.entity.principal.District;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;

/**
 * 代理员
 *
 * @author mohanlan
 */
@Entity
@Table(name = "agent")
@PersistentFormat
@JsonIgnoreProperties(value = {"hibernate_lazy_initializer", "handler", "fieldHandler"})
public class Agent {

    @Id
    @GeneratedValue
    private Long id;

    private String perfix;

    private String name;

    @Column(unique = true)
    private String mobile;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 所属分区
     */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private District district;

    /**
     * 关联公司
     */
    @ManyToOne(optional = true)
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @JsonIdentityReference(alwaysAsId = true)
    private AgentCompany company;

    private String openid;

    /**
     * 认证代码
     */
    private String authCode;

    /**
     * 密码
     */
    @JsonIgnore
    private String password;

    @Column(name = "is_disabled")
    private boolean disabled;

    /**
     * The default constructor
     */
    public Agent() {
        this.id = new Long(1);
    }

    /**
     * @param agentApply
     * @param company
     */
    public Agent(AgentApply agentApply, AgentCompany company) {
        this.name = agentApply.getName();
        this.email = agentApply.getEmail();
        this.perfix = agentApply.getAlias();
        this.district = agentApply.getDistrict();
        this.mobile = agentApply.getMobile();
        this.openid = agentApply.getOpenid();
        this.company = company;
        this.disabled = false;
    }

    /**
     * @return
     */
    public Agent createAgent() {
        this.disabled = false;
        return this;
    }

    /**
     * @return
     */
    public Agent freeze() {
        this.disabled = true;
        return this;
    }

    /**
     * @return
     */
    public Agent activate() {
        this.disabled = false;
        return this;
    }

    /**
     * 是否有密码
     * @return
     */
    @Transient
    public boolean isHasPassword(){
        return StringUtils.isNotBlank(this.password);
    }

    /**
     * @param agent
     * @return
     */
    public Agent edit(Agent agent) {
        this.email = StringUtils.isNotBlank(agent.getEmail()) ? agent.getEmail() : this.email;
        this.name = StringUtils.isNotBlank(agent.getName()) ? agent.getName() : this.name;
        this.mobile = StringUtils.isNotBlank(agent.getMobile()) ? agent.getMobile() : this.mobile;
        return this;
    }

    /**
     * @param openid
     * @return
     */
    public Agent bindOpenid(String openid) {
        this.openid = openid;
        return this;
    }

    /**
     * 初始化密码
     * @param password
     * @return
     */
    public Agent initPassword(String password){
        this.password = createPassword(password);
        return this;
    }

    /**
     * 创建密码
     * @return
     */
    private String createPassword(String password){
        String saltPassword = "{" + this.mobile + "}" + password;
        String pwd = DigestUtils.md5Hex(saltPassword);
        return pwd ;
    }

    /**
     * 检查密码
     * @return
     */
    public boolean checkPassword(String password){
        String pwd = createPassword(password);
        return StringUtils.equals(this.password, pwd);
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the perfix
     */
    public String getPerfix() {
        return perfix;
    }

    /**
     * @param perfix the perfix to set
     */
    public void setPerfix(String perfix) {
        this.perfix = perfix;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the district
     */
    public District getDistrict() {
        return district;
    }

    /**
     * @param district the district to set
     */
    public void setDistrict(District district) {
        this.district = district;
    }

    /**
     * @return the company
     */
    public AgentCompany getCompany() {
        return company;
    }

    /**
     * @param company the company to set
     */
    public void setCompany(AgentCompany company) {
        this.company = company;
    }

    /**
     * @return the openid
     */
    public String getOpenid() {
        return openid;
    }

    /**
     * @param openid the openid to set
     */
    public void setOpenid(String openid) {
        this.openid = openid;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
